package com.leyou.cart.controller;

import com.leyou.cart.entity.Cart;
import com.leyou.cart.entity.MyCollections;
import com.leyou.cart.service.CartService;
import com.leyou.common.pojo.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LT
 * @version 1.0.0
 * @ClassName CartController.java
 * @Description TODO 购物车视图层
 * @createTime 2020年05月20日 15:48:00
 */
@RestController
public class CartController {

    @Resource
    private CartService cartService;

    /**
     * 添加商品到购物车中
     *
     * @param cart 购物车对象接收参数
     * @return 没有返回值
     */
    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart) {
        cartService.addCart(cart);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 获取购物车数据
     *
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<Cart>> findCartList() {
        List<Cart> cartList = cartService.findCartList();
        return ResponseEntity.ok(cartList);
    }

    /**
     * 修改购物车数据
     *
     * @param id
     * @param num
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateNum(@RequestParam("id") Long id,
                                          @RequestParam("num") Integer num) {
        this.cartService.updateNum(id, num);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除购物车(根据skuId删除用户购物车商品)
     *
     * @param skuId
     * @return
     */
    @DeleteMapping("{skuId}")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId") String skuId) {
        this.cartService.deleteCart(skuId);
        return ResponseEntity.ok().build();
    }

    /**
     * 合并购物车，本质就是批量添加购物车
     *
     * @param cartList
     * @return
     */
    @PostMapping("/list")
    public ResponseEntity<Void> addCarts(@RequestBody List<Cart> cartList) {
        cartService.addCarts(cartList);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

}
